package rpcHandler

import (
	"context"
	"fmt"
	"gitee.com/zaiqiang231/lovesport-user-service/app/http"
	"gitee.com/zaiqiang231/lovesport-user-service/app/rpc/proto"
	"sync"
)

var (
	userSever     *UserSever
	onceUserSever sync.Once
)

type UserSever struct {
}

func GetUserSever() (*UserSever, error) {
	onceUserSever.Do(func() {
		userSever = &UserSever{}
	})

	if userSever == nil {
		return nil, fmt.Errorf("got nil server")
	}

	return userSever, nil
}

func (s *UserSever) TokenCheck(ctx context.Context, r *proto.TokenCheckRequest) (*proto.TokenCheckRequestResponse, error) {
	isValid, identity, err := http.GlobalJwtAuth.TokenCheck(r.Token)
	var message string
	if err != nil {
		message = err.Error()
	} else {
		message = ""
	}
	return &proto.TokenCheckRequestResponse{
		IsValid:  isValid,
		Identity: identity,
		Message:  message,
	}, nil
}
